package com.lwt.codetop.dp;

// 300. 最长递增子序列

/**
 * 子序列：不要求连续
 * 思路：dp---->LIS
 * 状态表示：f[i]: 以nums[i]结尾的最长上升子序列长度为f[i]
 * 状态转移：子序列是递增的，取决于前面的数nums[j]，
 *         且要求nums[i] > nums[j]：f[i] = max(f[i], f[j] + 1)
 *
 */
public class LongestIncreasingSubsequence {
    public static void main(String[] args) {
        // 构建测试用例
        int[] nums = new int[]{10,9,2,5,3,7,101,18};
        int res = lengthOfLIS(nums);
        System.out.println(res);
    }

    /**
     * 求LIS
     * @param nums
     * @return
     */
    private static int lengthOfLIS(int[] nums) {
        int res = 0;
        int n = nums.length;
        int[] f = new int[n + 10];
        for (int i = 0; i < n; i++) {
            f[i] = 1;
            for (int j = 0; j < i; j++) {
                if(nums[i] > nums[j])
                    f[i] = Math.max(f[i], f[j] + 1);
            }
            res = Math.max(res, f[i]);
        }
        return res;
    }
}
